<!-- 能源维度统计 -->
<template>
  <div class="iBox">
    <table>
      <thead>
        <tr>
          <td rowspan="2">能源品种</td>
          <td rowspan="2">计量单位</td>
          <td rowspan="2">实物量(总能耗)</td>
          <td colspan="2">因子指标</td>
          <td colspan="2">船舶及海洋工程装备造修企业碳排放核算指南</td>
        </tr>
        <tr>
          <td>碳排放因子</td>
          <td>碳排放量(tCO2)</td>
          <td>碳排放因子</td>
          <td>碳排放量(tCO2)</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in state.source">
          <td>{{ item.type }}</td>
          <td>{{ item.unit }}</td>
          <td>{{ item.material }}</td>
          <td>{{ item.szRatio }}</td>
          <td>{{ item.szNum }}</td>
          <td>{{ item.ratio }}</td>
          <td>{{ item.num }}</td>
        </tr>
        <tr>
          <td colspan="3">总碳排放量</td>
          <td colspan="2">{{ state.sum?.sumCShen?.toFixed(4) }}</td>
          <td colspan="2">{{ state.sum?.sumCOcean?.toFixed(4) }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script setup>
import {
  onBeforeMount,
  getCurrentInstance,
  reactive,
  ref,
  computed,
  watch,
} from "vue";
import _ from "lodash";
import { exportExcel, setCellStyle } from "@/utils/exportExcel";

const { proxy } = getCurrentInstance();
const props = defineProps(["repair"]);
const state = reactive({
  source: [],
  sum: null,
});

watch(
  () => props.repair,
  (repair) => {
    if (repair) {
      proxy.$axios
        .get("/workflow/stat/first", { params: { repairId: repair.repairId } })
        .then(({ success, data }) => {
          if (success) {
            const source = _.map(data?.list || [], (o) => ({
              type: o.energyCategory,
              unit: o.unit,
              material: o.energyAmount?.toFixed(4),
              szRatio: o.factorShen?.toFixed(4),
              szNum: o.cshen?.toFixed(4),
              ratio: o.factorOcean?.toFixed(4),
              num: o.cocean?.toFixed(4),
            }));
            state.source = source;
            state.sum = data.sum;
          }
        });
    }
  },
  { immediate: true, deep: true }
);

const _exportExcel = () => {
  exportExcel(`单船研究数据`, `单船研究数据`, 7, (worksheet) => {
    let i = 2;
    const head = [];

    const a2 = worksheet.getCell(`A${i}`);
    worksheet.mergeCells(`A${i}:A${i + 1}`);
    a2.value = "能源品种";
    head.push(a2);

    const b2 = worksheet.getCell(`B${i}`);
    worksheet.mergeCells(`B${i}:B${i + 1}`);
    b2.value = `计量单位`;
    head.push(b2);

    const c2 = worksheet.getCell(`C${i}`);
    worksheet.mergeCells(`C${i}:C${i + 1}`);
    c2.value = "实物量(总能耗)";
    head.push(c2);

    const d2 = worksheet.getCell(`D${i}`);
    worksheet.mergeCells(`D${i}:E${i}`);
    d2.value = "因子指标";
    head.push(d2);

    const d3 = worksheet.getCell(`D${i + 1}`);
    d3.value = "碳排放因子";
    head.push(d3);

    const e3 = worksheet.getCell(`E${i + 1}`);
    e3.value = "碳排放量(tCO2)";
    head.push(e3);

    const f2 = worksheet.getCell(`F${i}`);
    worksheet.mergeCells(`F${i}:G${i}`);
    f2.value = "船舶及海洋工程装备造修企业碳排放核算指南";
    head.push(f2);

    const f3 = worksheet.getCell(`F${i + 1}`);
    f3.value = "碳排放因子";
    head.push(f3);

    const g3 = worksheet.getCell(`G${i + 1}`);
    g3.value = "碳排放量(tCO2)";
    head.push(g3);

    head.forEach((cell) => {
      cell.fill = {
        type: "pattern",
        pattern: "solid",
        fgColor: { argb: "c0c0c0" }, // 设置背景色
      };
      setCellStyle(cell);
    });

    i++;

    _.each(state.source, (column) => {
      i++;
      const cellA = worksheet.getCell(`A${i}`);
      cellA.value = column.type;
      setCellStyle(cellA);

      const cellB = worksheet.getCell(`B${i}`);
      cellB.value = column.unit;
      setCellStyle(cellB);

      const cellC = worksheet.getCell(`C${i}`);
      cellC.value = column.material;
      setCellStyle(cellC);

      const cellD = worksheet.getCell(`D${i}`);
      cellD.value = column.szRatio;
      setCellStyle(cellD);

      const cellE = worksheet.getCell(`E${i}`);
      cellE.value = column.szNum;
      setCellStyle(cellE);

      const cellF = worksheet.getCell(`F${i}`);
      cellF.value = column.ratio;
      setCellStyle(cellF);

      const cellG = worksheet.getCell(`G${i}`);
      cellG.value = column.num;
      setCellStyle(cellG);
    });

    i++;
    const _f1 = worksheet.getCell(`A${i}`);
    worksheet.mergeCells(`A${i}:C${i}`);
    _f1.value = "总碳排放量";
    setCellStyle(_f1);
    const _f2 = worksheet.getCell(`D${i}`);
    worksheet.mergeCells(`D${i}:E${i}`);
    _f2.value = state.num?.sumCShen?.toFixed(4);
    setCellStyle(_f2);
    const _f3 = worksheet.getCell(`F${i}`);
    worksheet.mergeCells(`F${i}:G${i}`);
    _f3.value = state.num?.sumCOcean?.toFixed(4);
    setCellStyle(_f3);
  });
};

defineExpose({ exportExcel: _exportExcel });
</script>
<style lang="less" scoped>
.iBox {
  width: 100%;
  height: 100%;

  table {
    width: 100%;
    border: 1px solid #f8f8f8;
  }

  th,
  td {
    text-align: center;
    border: 1px solid #f8f8f8;
    padding: 4px;
  }
}
</style>
